package com.softlab.retailwholesalesystem.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.softlab.retailwholesalesystem.entity.OrderEntity;
import com.softlab.retailwholesalesystem.entity.OutInWarehouseEntity;
import com.softlab.retailwholesalesystem.service.IOutInWarehouseService;
import com.softlab.retailwholesalesystem.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/outinwarehouse")
public class OutInWarehouseController {
    @Autowired
    private IOutInWarehouseService Service;


    @GetMapping
    public JsonResult<List<OutInWarehouseEntity>> getAll(){
        QueryWrapper<OutInWarehouseEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("is_delete",0);
        List<OutInWarehouseEntity> outInWarehouseEntities = Service.list(wrapper);
        return new JsonResult<>(200,"入库单列表查询成功", outInWarehouseEntities);
    }

    @PostMapping
    public JsonResult<Void> add(@RequestBody OutInWarehouseEntity outInWarehouseEntity){
        Service.save(outInWarehouseEntity);
        return new JsonResult<>(200, "入库单增加成功");
    }


    @PutMapping
    public JsonResult<Void> change(@RequestBody OutInWarehouseEntity outInWarehouseEntity){
        boolean b = Service.updateById(outInWarehouseEntity);
        if (!b) return new JsonResult<>(404,"修改失败");
        if (outInWarehouseEntity.getState().equals("已完成")){
            Integer confirm = Service.confirm(outInWarehouseEntity);
            return new JsonResult<>(confirm, "入库单确认");
        }
        return new JsonResult<>(200, "入库单修改成功");

    }

    @DeleteMapping("{id}")
    public JsonResult<Void> delete(@PathVariable Long id){
        OutInWarehouseEntity outInWarehouseEntity=new OutInWarehouseEntity();
        outInWarehouseEntity.setOutInWarehouseId(id);
        outInWarehouseEntity.setIsDelete(1);
        Service.updateById(outInWarehouseEntity);
        return new JsonResult<>(200, "入库单删除成功");
    }

    @GetMapping("/search")
    public JsonResult<List<OutInWarehouseEntity>> getList(OutInWarehouseEntity outInWarehouseEntity){
        List<OutInWarehouseEntity> list = Service.search(outInWarehouseEntity);
        return new JsonResult<>(200, "入库单查找成功", list);
    }
}
